<?php


namespace app\admin\user\logic;


use app\common\logic\BaseLogic;
use app\model\user\User;
use app\model\user\UserBalanceLog;
use think\facade\Db;

class UserBalanceLogLogic extends BaseLogic
{
    public function __construct()
    {
        $this->model = new UserBalanceLog();
    }

    public function checkBalance($user_id, $balance)
    {
        $cur = User::where(['id' => $user_id])->value("balance");
        return $cur < $balance;
    }

    public function changeBalance($user_id, $type, $balance, $ext = [])
    {
        $before = User::where(['id' => $user_id])->value("balance");
        $this->model->save([
            "type" => $type,
            "user_id" => $user_id,
            "balance" => $balance,
            "before" => $before,
            "order_id" => $ext['order_id'] ?? "",
            "order_no" => $ext['order_no'] ?? "",
            "remark" => $ext['remark'] ?? "",
        ]);
        User::where(['id' => $user_id])->update([
            "balance" => Db::raw("balance + ({$balance})"),
        ]);

    }
}